<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-select v-model="dataForm.status" filterable placeholder="状态" @change="selectStatus">
        <el-option v-for="item in options" :key="item.status" :label="item.label" :value="item.status">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.free" filterable placeholder="状态" @change="selectFree">
        <el-option v-for="item in optionsFree" :key="item.status" :label="item.label" :value="item.status">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.all" filterable placeholder="全部组件" @change="selectAll">
        <el-option v-for="item in widgeThemeType" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.classifyId" filterable placeholder="全部分类" @change="selectClassify">
        <el-option v-for="item in classifyNameList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-select v-model="dataForm.typeId" filterable placeholder="全部类型" @change="selectTypes">
        <el-option v-for="item in widgetTypeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="关键词搜索" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('widge:widge:save')" type="primary" @click="addOrUpdateHandle1()">新增通用组件</el-button>
        <el-button v-if="isAuth('widge:widge:save')" type="primary" @click="addOrUpdateHandle2()">新增主题组件</el-button>
        <el-button v-if="isAuth('widge:widge:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="ID">
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="组件名称">
      </el-table-column>
      <el-table-column prop="classifyName" header-align="center" align="center" label="组件分类">
      </el-table-column>
      <el-table-column prop="typeName" header-align="center" align="center" label="组件类型">
      </el-table-column>
      <el-table-column label="尺寸">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.widgetSize === 'small'">小号组件(small)</el-tag>
          <el-tag type="warning" v-else-if="scope.row.widgetSize === 'medium'">中号组件(medium)</el-tag>
          <el-tag type="danger" v-else-if="scope.row.widgetSize === 'larege'">大号组件(larege)</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="image" header-align="center" align="center" label="组件图">
        <template slot-scope="scope">
          <el-image style="width: 90px; height: 60px" :src="scope.row.image + '?x-oss-process=image/resize,h_100,w_100'"
            fit="contain" />
        </template>
      </el-table-column>
      <el-table-column prop="preview" header-align="center" align="center" label="预览图">
        <template slot-scope="scope">
          <el-image style="width: 90px; height: 60px"
            :src="scope.row.preview + '?x-oss-process=image/resize,h_100,w_100'" fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="描述" header-align="center" style="width: 30%;">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="描述" width="500" trigger="hover">
            <div>{{ scope.row.describes }}</div>
            <span slot="reference">{{
      scope.row.describes.substr(0, 23) + "..."
    }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否免费">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.free === 1">免费</el-tag>
          <el-tag type="danger" v-else-if="scope.row.free === 2">VIP+广告</el-tag>
          <el-tag type="danger" v-else-if="scope.row.free === 3">仅Vip</el-tag>
          <el-tag type="danger" v-else-if="scope.row.free === 4">仅广告</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否热门">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.hot === 1">是</el-tag>
          <el-tag type="danger" v-else-if="scope.row.hot === 2">否</el-tag>
          <el-tag type="danger" v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="80" label="是否最新">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.newest === 1">是</el-tag>
          <el-tag type="danger" v-else-if="scope.row.newest === 2">否</el-tag>
          <el-tag type="danger" v-else>否</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="updated" header-align="center" align="center" label="更新时间">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="280" label="操作">
        <template slot-scope="scope">

          <el-button type="text" size="small" @click="widgeListHandler(scope.row.name)">查看列表</el-button>
          <el-button type="text" v-if="scope.row.themeId != null" size="small"
            @click="copyWidge(scope.row.id)">复制到通用组件</el-button>
          <el-button type="text" size="small" @click="widgeConfigHandler(scope.row.id)">组件配置</el-button>
          <template v-if="scope.row.status == 1">
            <el-button type="text" size="small" @click="itemSoldOut(scope.row.id)">下架</el-button>
          </template>
          <template v-if="scope.row.status == 2">
            <el-button type="text" size="small" @click="itemPutaWay(scope.row.id)">待上架</el-button>
          </template>
          <el-button type="text" size="small" @click="updateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="copyTemp(scope.row.id)">复制为模板</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="[src]" />

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update2 v-if="addOrUpdateVisible2" ref="addOrUpdate2" @refreshDataList="getDataList"></add-or-update2>

    <!-- 弹窗, 查看列表 -->
    <widge-list v-if="widgeListVisible" ref="widgeList" @refreshDataList="getDataList"></widge-list>
  </div>
</template>

<script>
import WidgeList from "./widge-list";
import AddOrUpdate from "./widge-add-or-update";
import AddOrUpdate2 from "./widge-add-or-update2";

export default {
  data () {
    return {
      options: [
        {
          status: "",
          label: "全部状态"
        },
        {
          status: "1",
          label: "已上架"
        },
        {
          status: "2",
          label: "已下架"
        }
      ],
      optionsFree: [
        {
          status: "",
          label: "全部状态"
        },
        {
          status: "1",
          label: "免费"
        },
        {
          status: "2",
          label: "收费"
        }
      ],
      widgeThemeType: [
        {
          value: "",
          label: "全部组件"
        },
        {
          value: "1",
          label: "通用组件"
        },
        {
          value: "2",
          label: "主题组件"
        }
      ],
      widgetTypeList: [
        {
          id: 1,
          name: "文字组件"
        },
        {
          id: 2,
          name: "时间组件"
        },
        {
          id: 3,
          name: "电量组件"
        },
        {
          id: 4,
          name: "日历组件"
        },
        {
          id: 5,
          name: "健康组件"
        },
        {
          id: 6,
          name: "快捷组件"
        },
        {
          id: 7,
          name: "计时组件"
        },
        {
          id: 8,
          name: "天气组件"
        },
        {
          id: 9,
          name: "动图组件"
        },
        {
          id: 10,
          name: "控制面板"
        },
        {
          id: 11,
          name: "手机信息"
        }
      ],
      classifyNameList: [],
      src: "",
      showViewer: false,
      dataForm: {
        key: "",
        all: "",
        free: "",
        status: "",
        typeId: "",
        classifyId: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      widgeListVisible: false,
      addOrUpdateVisible: false,
      addOrUpdateVisible2: false
    };
  },
  components: {
    WidgeList,
    AddOrUpdate,
    AddOrUpdate2
  },
  activated () {
    this.getDataList();
    this.getClassifyList();
    this.initWidgeTypeList();
  },
  methods: {
    onPreview (url) {
      this.src = url;
      this.showViewer = true;
    },
    closeViewer () {
      this.showViewer = false;
    },
    copyTemp (id) {
      this.$http({
        url: this.$http.adornUrl("/widge/widge/copyTemp"),
        method: "get",
        params: this.$http.adornParams({
          id: id,
        })
      }).then(({ data }) => {
        this.$message({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            this.getDataList();
          }
        });
      })
    },
    copyWidge (id) {
      this.$http({
        url: this.$http.adornUrl("/widge/widge/copyWidge"),
        method: "get",
        params: this.$http.adornParams({
          id: id,
        })
      }).then(({ data }) => {
        this.$message({
          message: "操作成功",
          type: "success",
          duration: 1500,
          onClose: () => {
            this.getDataList();
          }
        });
      })
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/widge/widge/distinctNameList"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          key: this.dataForm.key,
          all: this.dataForm.all,
          free: this.dataForm.free,
          status: this.dataForm.status,
          typeId: this.dataForm.typeId,
          classifyId: this.dataForm.classifyId
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records;
          this.totalPage = data.page.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    //获取分类列表
    getClassifyList () {
      this.classifyNameList = [
        {
          id: "",
          name: "全部分类"
        }
      ];
      this.$http({
        url: this.$http.adornUrl("/widge/widge-classify/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: 1000
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          let objList = data.page.list;
          objList.forEach(obj => {
            let classifyName = {
              id: obj.id,
              name: obj.name
            };
            this.classifyNameList.push(classifyName);
          });
        }
      });
    },
    initWidgeTypeList () {
      this.widgetTypeList = [
        {
          id: "",
          name: "全部类型"
        }
      ];
      this.$http({
        url: this.$http.adornUrl("/widge/widge-type/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 10000
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          let objList = data.page.list;
          objList.forEach(obj => {
            let classifyName = {
              id: obj.id,
              name: obj.name
            };
            this.widgetTypeList.push(classifyName);
          });
        }
      });
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val;
    },
    selectStatus (value) {
      this.dataForm.status = value;
      this.getDataList();
    },
    selectFree (value) {
      this.dataForm.free = value;
      this.getDataList();
    },
    selectAll (value) {
      this.dataForm.all = value;
      this.getDataList();
    },
    selectClassify (value) {
      this.dataForm.classifyId = value;
      this.getDataList();
    },
    selectTypes (value) {
      this.dataForm.typeId = value;
      this.getDataList();
    },
    // 查看列表
    widgeListHandler (name) {
      this.widgeListVisible = true;
      this.$nextTick(() => {
        this.$refs.widgeList.init(name);
      });
    },

    // 新增
    addOrUpdateHandle1 (id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 新增
    addOrUpdateHandle2 (id) {
      this.addOrUpdateVisible2 = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate2.init(id);
      });
    },
    // 修改
    updateHandle (id) {
      let widge = null;
      this.dataList.forEach((item, index) => {
        if (item.id === id) {
          widge = item;
        }
      });
      //通用组件
      if (widge.themeId == null) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id);
        });
      } else {
        //主题组件
        this.addOrUpdateVisible2 = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate2.init(id);
        });
      }
    },
    widgeConfigHandler (id) {
      this.$router.push({
        path: `widge-config`,
        query: {
          widgeId: id
        }
      });
    },
    // 下架
    itemSoldOut (id) {
      this.$confirm(`确定要对该组件进行下架操作操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/widge/widge/update"),
          method: "post",
          data: this.$http.adornData({
            id: id,
            status: 2
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 商品重新上架
    itemPutaWay (id) {
      this.$confirm(`确定要对该组件进行上架操作操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/widge/widge/update"),
          method: "post",
          data: this.$http.adornData({
            id: id,
            status: 1
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    },
    // 删除
    deleteHandle (id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
          return item.id;
        });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/widge/widge/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
